package com.example.take_out_admin.service.impl;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.example.take_out_admin.config.JwtConfig;
import com.example.take_out_admin.entity.Employee;
import com.example.take_out_admin.exceptions.BusinessException;
import com.example.take_out_admin.exceptions.enumetation.ResponseEnum;
import com.example.take_out_admin.mapper.EmployeeMapper;
import com.example.take_out_admin.service.EmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.take_out_admin.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.annotation.Resource;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;


@Service
@Slf4j
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Resource
    private HttpServletRequest request;

    @Resource
    private JwtConfig jwtConfig;
    @Override
    public String login(Employee employee) {
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername());

        Employee one = this.getOne(wrapper);
        //log.info("{}",one);
        if(one==null) {
            log.info("用户名:{} 不存在",employee.getUsername());
            throw new RuntimeException("用户名不存在或密码错误");
        }
        //密码明文->密码密文->比对
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        if(!one.getPassword().equals(password)){
            log.info("用户名:{},密码：{} 错误",employee.getUsername(),employee.getPassword());
            throw new RuntimeException("用户名不存在或密码错误");
        }
        //签发token
        String token = JwtUtils.createJwt(
                one.getId(),
                one.getUsername(),
                "",
                jwtConfig.getTtl(),
                jwtConfig.getKey()
        );

        log.info("token：{}",token);
        return token;
    }

    @Override
    public void updateEmployee(Employee employee) {
        String token = "";
        Cookie[] cookies = request.getCookies();
        if(cookies!=null){
            for(Cookie cookie : cookies){
                if(cookie.getName().equals("token")){
                    token=cookie.getValue();
                }
            }
        }
        log.info("token:{}",token);
        if(!JwtUtils.validateToken(token,jwtConfig.getKey())){
            throw new BusinessException(ResponseEnum.TOKEN_EXPIRED);
        }
        Claims claims = JwtUtils.parseJwt(token,jwtConfig.getKey());
        String name=claims.get("name",String.class);

        employee.setCreateUser(name);
        employee.setUpdateUser(name);

        Date now=new Date();
        employee.setCreateTime(now);
        employee.setUpdateTime(now);
        log.info("aaa{}",employee);
        this.updateById(employee);
    }

    @Override
    public void saveEmployee(Employee employee) {
        String token = "";
        Cookie[] cookies = request.getCookies();

        if (cookies != null) {
            for (Cookie cookie : cookies) {
//                System.out.println(cookie.getName()+"     "+cookie.getValue());
                if ("token".equals(cookie.getName())) {
                    token=cookie.getValue();
                }
            }
        }

        if(!JwtUtils.validateToken(token,jwtConfig.getKey())){
            throw new BusinessException(ResponseEnum.TOKEN_EXPIRED);
        }
        Claims claims = JwtUtils.parseJwt(token, jwtConfig.getKey());
        String name = claims.get("name", String.class);

        employee.setCreateUser(name);
        employee.setUpdateUser(name);

        Date now=new Date();
        employee.setCreateTime(now);
        employee.setUpdateTime(now);

        this.save(employee);
    }

    @Override
    public boolean validateToken(String token) {
        return JwtUtils.validateToken(token, jwtConfig.getKey());
    }
}
